
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;


namespace Swaf.Call
{
	/// <summary>
	/// Summary description for RuleMgrCall.
	/// </summary>
	public class RuleMgrCall : BaseCall
	{
		public RuleMgrCall(string id, XmlElement initInfo)
			:base(id, initInfo)
		{
		}
		public RuleMgrCall(IBizObj ruleDef) : base(ruleDef)
		{
		}

		public RuleMgrCall(string id, string ruleName, params object[] p)
			:base(id)
		{
			m_methodName = ruleName;
			m_baseParams = p;
		}

		public override object call(params object[] p)
		{
			IApplication app = Application.currentApp;
			if(p.Length > 0 && p[0] is IApplication)
				app = (IApplication)p[0];
			try
			{
				IResults rc = app.ruleMgr.execute(m_methodName, this.calcCallParams(p));
				if(rc.ExceptionInfo == null)
					return rc.CallResults;
				else
					throw new CallException("Rule call failed", rc.ExceptionInfo);
			}
			catch(Exception e)
			{
				throw new CallException(String.Format("Unable to make call to rule '{0}'", m_methodName),e);
			}
		}

	}
}
